#include "DXUT.h"
#include "Material.h"

D3DCOLORVALUE  DefaultAmbientColor=D3DXCOLOR(D3DCOLOR_XRGB(40, 40, 40));
D3DCOLORVALUE  DefaultDiffuseColor=D3DXCOLOR(D3DCOLOR_XRGB(210, 210, 210));
D3DCOLORVALUE  DefaultSpecularColor=D3DXCOLOR(D3DCOLOR_XRGB(255, 255, 255));
CMaterial::CMaterial(void)
{
	SetDefaultD3DMaterial();

}

 

CMaterial::CMaterial( LPCTSTR strDiffsuseTexture,LPCTSTR strNormalTexture ,LPCTSTR sttHeightTexture,LPTSTR strEnvienmentTexture ,bool bSetMaterial)
{

	m_bSetMaterial=bSetMaterial;
	SetDefaultD3DMaterial();

if(strDiffsuseTexture)
{
	m_pDiffuseTexture=new CFileTexture(strDiffsuseTexture,strDiffsuseTexture);
	Singleton<CGraphicsResourceManager>::Instance()->Add((void**) & m_pDiffuseTexture);
}
if(strNormalTexture)
{
	m_pNormalTexture=new CFileTexture(strNormalTexture,strNormalTexture);
	Singleton<CGraphicsResourceManager>::Instance()->Add((void**) & m_pNormalTexture);
}
if(sttHeightTexture)
{
	m_pHeightTexture=new CFileTexture(sttHeightTexture,sttHeightTexture);
	Singleton<CGraphicsResourceManager>::Instance()->Add((void**) & m_pHeightTexture);
}
if(strEnvienmentTexture)
{
	m_pEnvimentTexture=new CCubeTexture(strEnvienmentTexture,strEnvienmentTexture);
	Singleton<CGraphicsResourceManager>::Instance()->Add((void**) & m_pEnvimentTexture);
}

}


CMaterial::~CMaterial(void)
{
	if(m_pDiffuseTexture)
	{
		Singleton<CGraphicsResourceManager>::Instance()->RemoveAndDestroy(m_pDiffuseTexture);
     
	}

	if(m_pNormalTexture)
	{
 
	Singleton<CGraphicsResourceManager>::Instance()->RemoveAndDestroy(m_pNormalTexture);
	}
	if(m_pHeightTexture)
	{

		Singleton<CGraphicsResourceManager>::Instance()->RemoveAndDestroy(m_pHeightTexture);
	}
	if(m_pEnvimentTexture)
	 	Singleton<CGraphicsResourceManager>::Instance()->RemoveAndDestroy(m_pEnvimentTexture);
	 
}

void CMaterial::SetDefaultD3DMaterial()
{
	m_d3dMaterial.MatD3D.Ambient=DefaultAmbientColor;
	m_d3dMaterial.MatD3D.Diffuse=DefaultDiffuseColor;
	m_d3dMaterial.MatD3D .Specular = DefaultSpecularColor;
	m_d3dMaterial.MatD3D.Power=24.0f;
	m_d3dMaterial.MatD3D.Emissive=DefaultDiffuseColor;
	m_pDiffuseTexture=NULL;
	m_pNormalTexture=NULL;
	m_pHeightTexture=NULL;
	m_pEnvimentTexture=NULL;
}

void CMaterial::ApplyMaterial( CMaterialShader * pShader,const D3DXMATRIX  * matWorld,const D3DXMATRIX * matViewInverse, const  D3DXMATRIX  * matViewProj) 
{
	assert(pShader);
	if(m_bSetMaterial)
	{
 pShader->SetAmbientColor(m_d3dMaterial.MatD3D.Ambient);
 pShader->SetDiffuseColor(m_d3dMaterial.MatD3D.Diffuse);
 pShader->SetSpecularColor(m_d3dMaterial.MatD3D.Specular);
 pShader->SetSpecularPower(m_d3dMaterial.MatD3D.Power);
	}

 if(matWorld)
 pShader->SetWorldMatrix(*matWorld);
 if(matViewProj)
 pShader->SetViewProjMatrix  (*matViewProj);
 if(matViewInverse)
 pShader->SetViewInverseMatrix(*matViewInverse);
 if(m_pDiffuseTexture)
 {
	 pShader->SetDiffseTexture((LPDIRECT3DTEXTURE9)m_pDiffuseTexture->GetTexture());
 }

 if(m_pNormalTexture)
 {
	 pShader->SetNormalTexture((LPDIRECT3DTEXTURE9)m_pNormalTexture->GetTexture());
 }
 if(m_pHeightTexture)
 {
	 pShader->SetHeightTexture((LPDIRECT3DTEXTURE9)m_pHeightTexture->GetTexture());
 }
 if(m_pEnvimentTexture)
	 pShader->SetCubeTexure((LPDIRECT3DCUBETEXTURE9)m_pEnvimentTexture->GetTexture());
}

 